﻿Public Class MyPanel
    Inherits Panel

    Private m_visible As UIElement = Nothing

    Private WithEvents m_children As UIElementCollection


    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
        If InternalChildren.Count = 0 Then Return
        If m_visible Is Nothing Then m_visible = InternalChildren.Item(0)
        Dim nextvisible As UIElement = Nothing
        Dim nchildren As Integer = InternalChildren.Count
        Dim visindex As Integer = InternalChildren.IndexOf(m_visible)
        If visindex = nchildren - 1 Then
            nextvisible = InternalChildren.Item(0)
        Else
            nextvisible = InternalChildren.Item(visindex + 1)
        End If
        m_visible.Visibility = Windows.Visibility.Collapsed
        nextvisible.Visibility = Windows.Visibility.Visible
        m_visible = nextvisible
        InvalidateArrange()
        MyBase.OnMouseDown(e)
    End Sub

    Protected Overrides Function MeasureOverride(ByVal availableSize As System.Windows.Size) As System.Windows.Size
        Dim childSize As Size = availableSize
        For Each child As UIElement In InternalChildren
            If child.IsVisible Then child.Measure(childSize)
        Next
        Return MyBase.MeasureOverride(availableSize)
    End Function

    Protected Overrides Function ArrangeOverride(ByVal finalSize As System.Windows.Size) As System.Windows.Size
        Static i As Integer = 0
        For Each child As UIElement In InternalChildren
            If Not child.IsVisible Then Continue For
            Dim x As Double = i * 5
            Dim y As Double = i * 5
            child.Arrange(New Rect(New System.Windows.Point(x, y), child.DesiredSize))
            i += 1
        Next
        Return MyBase.ArrangeOverride(finalSize)
    End Function
End Class
